﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace AppManager.Web
{
    /// <summary>
    /// mvc 頁面基類，需要在web.config中單獨配置
    /// 主要是可以放一些基本屬性，方便頁面中直接使用
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    public abstract class BaseView<TModel> : System.Web.Mvc.WebViewPage<TModel>
    {
        /// <summary>
        /// 默認沒有圖片時顯示
        /// </summary>
        public string noImage = "http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image";

        /// <summary>
        /// 返回圖片的絕對路徑
        /// </summary>
        /// <param name="imgUrl">圖片相對路徑</param>
        /// <param name="defaultImg">缺省時的默認圖片</param>
        /// <returns></returns>
        public string GetImage(string imgUrl, string defaultImg = null)
        {
            if (string.IsNullOrEmpty(imgUrl))
            {
                if (defaultImg == null)
                    return noImage;
                return defaultImg;
            }

            //url.content會在為null或空時報錯
            return Url.Content(imgUrl);
        }
        /// <summary>
        /// 返回文件的絕對路徑
        /// </summary>
        /// <param name="url">相對路徑</param>
        /// <returns></returns>
        public string GetFileUrl(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return string.Empty;
            }
            return Url.Content(url);
        }

        /// <summary>
        /// 替換文本
        /// </summary>
        /// <param name="strHtml"></param>
        /// <returns></returns>
        private string Translate(string strHtml)
        {
            Regex regex = new Regex(@"\[[-0-9a-zA-Z]+\]");
            var matchs = regex.Matches(strHtml);
            if (matchs.Count > 0)//匹配到
            {
                var flagList = new List<string>(matchs.Count);
                foreach (Match match in matchs)//循環收集標識
                {
                    flagList.Add(match.Groups[0].Value.Replace("[", "").Replace("]", ""));
                }

                foreach (var item in flagList)
                {
                    //var value = ModuleTranslate.Get(item);
                    //strHtml = strHtml.Replace(string.Format("[{0}]", item), value);
                }
            }
            return strHtml;
        }

        /// <summary>
        /// 重寫這個方法來替換文本
        /// </summary>
        public override void ExecutePageHierarchy()
        {
            // Replace output stream with a fake local stream
            StringWriter fakeOutput = new StringWriter();

            // Save output stack top level stream, and replace with fake local stream
            TextWriter outputStackTopOutput = OutputStack.Pop();
            OutputStack.Push(fakeOutput);

            // Run Razor view engine
            base.ExecutePageHierarchy();

            string content = fakeOutput.ToString();

            //content = Translate(content);

            // Set back real outputs, and write to the real output 
            OutputStack.Pop();
            OutputStack.Push(outputStackTopOutput);
            outputStackTopOutput.Write(content);
        }
    }
}
